package com.framework.websocket.example;

import com.framework.websocket.core.SubscriptionConfig;
import com.framework.websocket.redis.RedisEventManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 集成使用示例
 * 展示如何在其他项目中使用 WebSocket Redis 框架
 */
@RestController
@RequestMapping("/api/example")
public class IntegrationExample {
    
    @Autowired
    private RedisEventManager redisEventManager;
    
    @Autowired
    private SubscriptionConfig subscriptionConfig;
    
    /**
     * 示例1：更新设备状态
     * 更新 Redis 后会自动推送给订阅了 DEVICE_STATUS 的客户端
     */
    @PostMapping("/device/{deviceId}/status")
    public Map<String, Object> updateDeviceStatus(
            @PathVariable String deviceId,
            @RequestParam boolean online,
            @RequestParam(required = false) Double temperature) {
        
        // 构建设备状态数据
        Map<String, Object> deviceStatus = new HashMap<>();
        deviceStatus.put("deviceId", deviceId);
        deviceStatus.put("online", online);
        deviceStatus.put("temperature", temperature);
        deviceStatus.put("updateTime", System.currentTimeMillis());
        
        // 获取键前缀
        String keyPrefix = subscriptionConfig.getKeyPrefix(SubscriptionConfig.TYPE_DEVICE_STATUS);
        String key = keyPrefix + deviceId;
        
        // 更新 Redis，自动触发推送
        redisEventManager.setWithExpire(key, deviceStatus, 3600); // 1小时过期
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", deviceStatus);
        return result;
    }
    
    /**
     * 示例2：发送告警
     */
    @PostMapping("/alarm/send")
    public Map<String, Object> sendAlarm(
            @RequestParam String deviceId,
            @RequestParam String alarmType,
            @RequestParam String message) {
        
        // 构建告警数据
        Map<String, Object> alarmData = new HashMap<>();
        alarmData.put("deviceId", deviceId);
        alarmData.put("alarmType", alarmType);
        alarmData.put("message", message);
        alarmData.put("level", "WARNING");
        alarmData.put("timestamp", System.currentTimeMillis());
        
        // 获取键前缀
        String keyPrefix = subscriptionConfig.getKeyPrefix(SubscriptionConfig.TYPE_ALARM);
        String key = keyPrefix + deviceId + ":" + System.currentTimeMillis();
        
        // 更新 Redis，自动触发推送
        redisEventManager.setWithExpire(key, alarmData, 86400); // 24小时过期
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("alarm", alarmData);
        return result;
    }
    
    /**
     * 示例3：发送系统通知
     */
    @PostMapping("/system/notify")
    public Map<String, Object> sendSystemNotification(
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam(defaultValue = "INFO") String level) {
        
        // 构建通知数据
        Map<String, Object> notification = new HashMap<>();
        notification.put("title", title);
        notification.put("content", content);
        notification.put("level", level);
        notification.put("timestamp", System.currentTimeMillis());
        
        // 获取键前缀
        String keyPrefix = subscriptionConfig.getKeyPrefix(SubscriptionConfig.TYPE_SYSTEM_NOTIFY);
        String key = keyPrefix + System.currentTimeMillis();
        
        // 更新 Redis，自动触发推送
        redisEventManager.setWithExpire(key, notification, 300); // 5分钟过期
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("notification", notification);
        return result;
    }
    
    /**
     * 示例4：注册新的订阅类型
     */
    @PostMapping("/subscription/register")
    public Map<String, Object> registerNewSubscriptionType(
            @RequestParam String type,
            @RequestParam String keyPrefix,
            @RequestParam String description) {
        
        // 动态注册新的订阅类型
        subscriptionConfig.registerSubscriptionType(type, keyPrefix, description);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "订阅类型注册成功");
        result.put("type", type);
        result.put("keyPrefix", keyPrefix);
        return result;
    }
    
    /**
     * 获取所有订阅类型
     */
    @GetMapping("/subscription/types")
    public Map<String, Object> getSubscriptionTypes() {
        Map<String, Object> result = new HashMap<>();
        result.put("types", subscriptionConfig.getTypeDescriptions());
        result.put("mappings", subscriptionConfig.getTypeToPrefixMap());
        return result;
    }
}